package core;

import java.net.*;
import java.io.*;
import game.GameEngine;
import UserInterfase.SimpleGUI;
import UserInterfase.UserInterface;
import UserInterfase.SimpleBoard;
import UserInterfase.UITools;
import javax.swing.JOptionPane;

public class IvoCheckers {

	/** The version of the program. */
	public static final String IVOCHECKERS_VERSION = "0.1.0";

	/** The user interface to be used with the program. */
	private static UserInterface frontEnd;
	
	/** Tells the system if debug is on. */
	private static boolean debugOn = false;
	
	/** Tells the system if silent mode is on. */
	private static boolean silentMode = false;
	
	/** The currently played game. */
	private static GameEngine currentGame;

        private static int port = 5555;
        public static boolean gameEnd = false;

        public static UserInterface getFrontEnd()
        {
            return frontEnd;
        }
	/** Starts up and initializes the program based on the settings that were
	 * previously saved.
	 * @param args the command line arguments
	 */
	public static void main(String[] args) {
		/* Takes in the arguements and deals with them. */
		try {
			if (args.length == 0) {
				interfaceInit();
				interfaceDisplay();
			} 

		} /* Whoa!!! User is really trying to kill the program! */
		catch (StringIndexOutOfBoundsException err) {
			quit();
			/* When something just plain wierd happens... */
		} catch (Exception err) {
			err.printStackTrace();
			quit();
		}
	}

	/** Exits the program. */
	public static void quit() {
		System.exit(0);
	}

	/** Initializes and loads up the user interface for IvoCheckers.*/
	public static void interfaceInit() {
		frontEnd = new SimpleGUI();
		frontEnd.initUI();
	}
	
	/** Displays an initialized user interface for IvoCheckers. 
	 * Requires: frontEnd != null 
	 */
	public static void interfaceDisplay() {
		frontEnd.displayUI();
	}


	/** Saves the settings for the program. */
	public static void saveSettings() {
	}
	
	/** Starts a new game.
	 */
	public static void startNewGame() {
                gameEnd = false;
		currentGame = new GameEngine();  // Current until diff. game types are added.
		frontEnd.clearBoard();
		frontEnd.placePieces();
	}

        public static boolean startNewNetGame(ServerSocket serverSocket) {
            ClientReceiver recv = null;
            Socket clientSocket = null;
            try {
                recv = new ClientReceiver();
                recv.start();

                clientSocket = serverSocket.accept();
            } catch (IOException ex) {
                ex.printStackTrace();
                recv.stopThread();
                return false;
            }
            recv.stopThread();
            System.out.println("Connect successful!");
            try {
                PlayNetGame.setMSocketReader(new BufferedReader(
                        new InputStreamReader(clientSocket.getInputStream())));
                SimpleBoard.setMSocketWriter(new PrintWriter(
                        new OutputStreamWriter(clientSocket.getOutputStream())));
                PlayNetGame.setSimpleBoard(SimpleGUI.getBoard());
            }
            catch (IOException ex)
            {
                System.out.println("Can not create reader and writer for socket!");
                ex.printStackTrace();
                return false;       
            }
            return true;
        }
        
	public static void findGame() {
            UserInterfase.JoinGameWindow separate_window = 
                    new UserInterfase.JoinGameWindow();
            separate_window.initUI((int)UITools.getCenterOfScreen().getX(), 
                    (int)UITools.getCenterOfScreen().getY());
	}

	/** Gets the game engine (and game) that is currently being used.
	 * 
	 * Mini-TODO:
	 * !!! the game engine has to be separated from the Board.
	 * @return the current game.
	 */
	public static GameEngine getGame() {
		return IvoCheckers.currentGame;
	}
	
	/** Ends the game.
	 * Requires: getGame() != null
	 * Mini-TODO:
	 * - handling if there is no current game.
	 * @throws NoCurrentGameException if no game is currently running.
	 */
	public static void endGame() {
                gameEnd = true;
		frontEnd.endGame();
		GameEngine currentGame = null;
	}
}
